var EventEmitter = require('events').EventEmitter; 
var event = new EventEmitter(); 

const pool={
    allPrizePool:[{
        "image": "https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/32ed6a7619934144882d841761b63d3c~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "100矿石",
        "value": 100,
        "object": "stone_coin",
        "type": "prize",
        "checked": true,
        "probability":0.1,
        "description": ""
    }, 
    {
        "image": "https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/32ed6a7619934144882d841761b63d3c~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "200矿石",
        "value": 200,
        "object": "stone_coin",
        "type": "prize",
        "checked": true,
        "probability":0.1,
        "description": ""
    }, 
    {
        "image": "https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/32ed6a7619934144882d841761b63d3c~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "66矿石",
        "value": 66,
        "object": "stone_coin",
        "type": "prize",
        "checked": true,
        "probability":0.2,
        "description": ""
    }, 
    {
        "image": "https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0a4ce25d48b8405cbf5444b6195928d4~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "bug",
        "object": "real",
        "type": "prize",
        "checked": true,
        "probability":0.2,
        "description": ""
    }, 
    {
        "image": "https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c78f363f41a741ffa11dcc8a92b72407~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "掘金限量桌垫",
        "object": "real",
        "type": "prize",
        "checked": true,
        "probability":0.15,
        "description": ""
    }, 
    {
        "image": "https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/71c68de6368548bd9bd6c8888542f911~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "随机限量徽章",
        "object": "real",
        "type": "prize",
        "checked": true,
        "probability":0.15,
        "description": ""
    }, 
    {
        "image": "https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/33f4d465a6a9462f9b1b19b3104c8f91~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "Yoyo抱枕",
        "object": "real",
        "type": "prize",
        "checked": true,
        "probability":0.05,
        "description": ""
    }, 
    {
        "image": "https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5bf91038a6384fc3927dee294a38006b~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "掘金新款T恤",
        "object": "real",
        "type": "prize",
        "checked": true,
        "probability":0.05,
        "description": ""
    }, 
    {
        "image": "https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/aabe49b0d5c741fa8d92ff94cd17cb90~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "乐高海洋巨轮",
        "object": "real",
        "type": "prize",
        "checked": false,
        "probability":0,
        "description": ""
    }, 
    {
        "image": "https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4decbd721b2b48098a1ecf879cfca677~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp",
        "text": "switch",
        "object": "real",
        "type": "prize",
        "checked": false,
        "probability":0,
        "description": ""
    }
    ],
    prizePool:[
        {
          image: 'https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/32ed6a7619934144882d841761b63d3c~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp',
          text: '100矿石',
          value: 100,
          object: 'stone_coin',
          type: 'prize',
          probability: 0.5,
          description: ''
        },
        {
          image: 'https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/32ed6a7619934144882d841761b63d3c~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp',
          text: '200矿石',
          value: 200,
          object: 'stone_coin',
          type: 'prize',
          probability: 0.1,
          description: ''
        },
        {
          image: 'https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/32ed6a7619934144882d841761b63d3c~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp',
          text: '66矿石',
          value: 66,
          object: 'stone_coin',
          type: 'prize',
          probability: 0.1,
          description: ''
        },
        {
          image: 'https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0a4ce25d48b8405cbf5444b6195928d4~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp',
          text: 'bug',
          object: 'real',
          type: 'prize',
          probability: 0.1,
          description: ''
        },
        {
          image: 'https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/c78f363f41a741ffa11dcc8a92b72407~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp',
          text: '掘金限量桌垫',
          object: 'real',
          type: 'prize',
          probability: 0.05,
          description: ''
        },
        {
          image: 'https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/71c68de6368548bd9bd6c8888542f911~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp',
          text: '随机限量徽章',
          object: 'real',
          type: 'prize',
          probability: 0.05,
          description: ''
        },
        {
          image: 'https://p1-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/33f4d465a6a9462f9b1b19b3104c8f91~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp',
          text: 'Yoyo抱枕',
          object: 'real',
          type: 'prize',
          probability: 0.05,
          description: ''
        },
        {
          image: 'https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/5bf91038a6384fc3927dee294a38006b~tplv-k3u1fbpfcp-no-mark:0:0:0:0.awebp',
          text: '掘金新款T恤',
          object: 'real',
          type: 'prize',
          probability: 0.05,
          description: ''
        }
    ],
    poolProb:[0.1 , 0.1 ,  0.2,
              0.2 , 0.15, 0.15,
              0.05, 0.05],
    arr:[0.1,
        0.2,
        0.4,
        0.6,
        0.75,
        0.9,
        0.95,
        1.0],
}

function updatePrizePool(){
    let newPrizePool=new Array();
    for (let i=0;i < pool.allPrizePool.length;i++)
    {
        if ( pool.allPrizePool[i].checked === true )
        {
            newPrizePool.push(pool.allPrizePool[i]);
        }
    }
    pool.prizePool= newPrizePool;

    let newPoolProb = new Array;
    newPrizePool.forEach((item) => {
        newPoolProb.push(item.probability)
    })
    pool.poolProb = newPoolProb;


    let newArr = new Array();
    newPoolProb.reduce((accumulator , cur) => {
        accumulator += cur;
        newArr.push( accumulator );
        return accumulator;
    } ,0 );
    console.log(newArr);
    pool.arr = newArr;
}

event.on('allPrizePool_updated', function() { 
    updatePrizePool()
    console.log('pool_updated'); 
}); 

// user 的用户名和密码
var userdata = {
  user1:{
      username : 'user1',
      password : '123456',
      stone : 1000,
      prizeList : [],
      lastSignin : undefined,
  },
  user2:{
      username : 'user2',
      password : '123456',
      stone : 1000,
      prizeList : [],
      lastSignin : undefined,
  },
}

// admin 的用户名和密码
var admindata = {
  admin:{
      username : 'admin',
      password : 'admin'
  },
}


var activeAdmin = null;
var activeUser = null;

function userHasLogin(){
  return activeUser !== null;
}
function adminHasLogin(){
  return activeAdmin !== null;
}

function login(username, password){
    if (username in userdata && userdata[username].password === password)
    {
        activeUser = userdata[username];
        return true;
    }
    else 
        return false;
}

function adminLogin(username,password) {
    if (username in admindata && admindata[username].password === password)
    {
        activeAdmin = admindata[username];
        return true;
    }
    else 
        return false;
}

function canSignin() {
    let nowDate = new Date().getDate();
    return nowDate != activeUser.lastSignin;
}

function signin(){
    if (!canSignin())
    {
        return false;
    }
    else {
        let nowDate = new Date().getDate();
        activeUser.lastSignin = nowDate;
        activeUser.stone += 100;
        return true;
    }
}
function getStoneNum(){
    return activeUser.stone;
}

function getPrizeList(){
    return activeUser.prizeList;
}

function draw(){
    if(hasEnoughStone()){
        activeUser.stone -= 200;
        index = getLuckyNumber();
        return index;
    }
    else
        return (-1);
}

function hasEnoughStone(){
    if(activeUser.stone < 200)
    {
        return false;
    }
    else{
        return true;
    }
}

function getLuckyNumber() {
    let num = Math.random();
    for (let i = 1; i < pool.arr.length; i++) {
        if (num <= pool.arr[i]) 
            return i;
    }
}

function getPrizePool(){
    return pool.prizePool;
}
function addToPrizeList(prize){
    // 如果奖品是钻石则直接领奖
    if(prize.object === 'stone_coin' ){
        prize.isReceived = true; 
        activeUser.stone += parseInt(prize.value);
    }
    else{
        prize.isReceived = false; 
    }
    activeUser.prizeList.push(prize);
}

function getPrizeByIndex(index){
    return pool.prizePool[index];
}


// 以下为admin的函数
function getAllPrizePool(){
    return pool.allPrizePool;
}
function setAllPrizePool(newAllPool){
    pool.allPrizePool = newAllPool;
    event.emit('allPrizePool_updated'); 
}


module.exports = {
    getPrizePool,
    addToPrizeList,
    signin,
    canSignin,
    getStoneNum,
    getPrizeList,
    draw,
    getPrizeByIndex,
    getAllPrizePool,
    setAllPrizePool,
    login,
    adminLogin,
    userHasLogin,
    adminHasLogin
};